/**
 * Copyright 2013-2015
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */	  

package com.cos.controller.admin.company;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cos.controller.base.BaseController;
import com.cos.entity.Page;
import com.cos.service.admin.company.CompanyManagementService;
import com.cos.util.Const;
import com.cos.util.PageData;

/**
 * TODO 类或接口说明
 * <p/>
 * 
 * @author <a href="15083378620@163.com">LXJ</a>
 * @version  Date: 2016年5月13日 上午11:15:25
 * @serial 1.0
 * @since 2016年5月13日 上午11:15:25
 */

@Controller
@RequestMapping(value="/companyManagement")
public class CompanyManagementController extends BaseController{
	
	
	String menuUrl = "companyManagement/selectCompanyManagement.do";
	@Resource(name="companyManagementService")
	private CompanyManagementService companyManagementService;
	
	
	/*
	 * 公司成员管理
	 */
	@RequestMapping(value="/selectCompanyManagement")
	public Object selectCompanyManagement(Page page) throws Exception{
		logBefore(logger,"公司成员展示");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			page.setPd(pd);
			Subject currentUser = SecurityUtils.getSubject();  
			Session session = currentUser.getSession();
			pd = this.getPageData();
			String C_ID =  session.getAttribute("C_ID").toString();
			List<PageData> u_Id = companyManagementService.SelectU_ID(C_ID);
			String[] USER_IDS = companyManagementService.groupIDs(u_Id);
			List<PageData> list_User = companyManagementService.selectAllU(USER_IDS);
			mv.setViewName("admin/usercompany/companyuser_list");
			mv.addObject("varList", list_User);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.toString());
		}finally{
			logAfter(logger);
		}
		return mv;
	}
	

	/*
	 * 公司部门管理
	 */
	@RequestMapping(value="/selectCompanyDepartment")
	public Object selectCompanyDepartment(Page page) throws Exception{
		logBefore(logger,"公司部门展示");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			page.setPd(pd);
			Subject currentUser = SecurityUtils.getSubject();  
			Session session = currentUser.getSession();
			pd = this.getPageData();
			String C_ID =  session.getAttribute("C_ID").toString();
			List<PageData> name_list = companyManagementService.selectCompanyDepartment(C_ID);
			mv.setViewName("admin/usercompany/companydepartment_list");
			mv.addObject("varList", name_list);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.toString());
		}finally {
			logAfter(logger);
		}
		return mv;
	}
			
			
			
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
}
